package org.cttic.visualization.visualization.util;

import com.alibaba.fastjson.JSONObject;
import org.cttic.visualization.visualization.common.retSectionData;
import org.cttic.visualization.visualization.common.sectionBillQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @author shkstart
 * @create 2020-05-25 13:32
 */
@Component
public class chargingUtil {
    @Autowired
    private VehicleInformationUtil vehicleInformationUtil;
    public String queryTransFee(String url){
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = null;
        try {
            responseEntity =restTemplate.getForEntity(url, String.class);
        }catch (Exception e){
            return null;
        }

        String body = responseEntity.getBody();
        retSectionData retSectionData = JSONObject.parseObject(body, retSectionData.class);
        //单位为分 需转换成元
        String receivableAmount = retSectionData.getData().getReceivableAmount();//应收标准费用
        String billAmount = retSectionData.getData().getBillAmount();//实际应收费用
        String discountAmount = retSectionData.getData().getDiscountAmount();//优惠金额
        float bill = Float.parseFloat(billAmount)/100;

        //System.out.println("应收标准费用:"+Float.parseFloat(receivableAmount)/100);
        //System.out.println("实际应收费用:"+bill);
        //System.out.println("优惠金额:"+Float.parseFloat(discountAmount)/100);
        return bill+"";
    }
    public String queryBillAm(String vehPlate,String vehPlateColor,String provinceId,String sectionCode,
                              String direction,String billMile,String transTime,String tollIntervals){
        //先查车辆信息库
        sectionBillQuery sectionBillQuery = new sectionBillQuery();
        vehicleInformationUtil.informationQuery(vehPlate,vehPlateColor,sectionBillQuery);
        String mediumType = sectionBillQuery.getMediumType();   //通行类型
        Integer vehicleType = sectionBillQuery.getVehicleType();//车型
        Integer vehCode = sectionBillQuery.getVehCode();        //车种
        Integer actualAxleNum = sectionBillQuery.getActualAxleNum();//轴数

        if(mediumType!=null&&vehicleType!=null&&vehCode!=null){
            String urlString=null;
            if(actualAxleNum!=null){
                urlString ="http://192.168.195.30:9042/hmop/mmp/api/tb/getJiangXiNationalBilling?vehPlate="+vehPlate+"&plateColor="+vehPlateColor+
                        "&adminCode="+provinceId+"&sectionCode="+sectionCode+
                        "&sectionDirection="+direction+"&billMile="+billMile+
                        "&currentsTime="+transTime+"&vehCode="+vehCode+
                        "&vehicleType="+vehicleType+"&actualAxleNum="+actualAxleNum+
                        "&mediumType="+mediumType+"&tollIntervals="+tollIntervals;
            }else {
                urlString ="http://192.168.195.30:9042/hmop/mmp/api/tb/getJiangXiNationalBilling?vehPlate="+vehPlate+"&plateColor="+vehPlateColor+
                        "&adminCode="+provinceId+"&sectionCode="+sectionCode+
                        "&sectionDirection="+direction+"&billMile="+billMile+
                        "&currentsTime="+transTime+"&vehCode="+vehCode+
                        "&vehicleType="+vehicleType+"&actualAxleNum="+3+
                        "&mediumType="+mediumType+"&tollIntervals="+tollIntervals;
            }
            //System.out.println("url:"+urlString);
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(urlString, String.class);
            String body = responseEntity.getBody();
            retSectionData retSectionData = JSONObject.parseObject(body, retSectionData.class);
            //单位为分 需转换成元
            String receivableAmount = retSectionData.getData().getReceivableAmount();//应收标准费用
            String billAmount = retSectionData.getData().getBillAmount();//实际应收费用
            String discountAmount = retSectionData.getData().getDiscountAmount();//优惠金额
            float bill = Float.parseFloat(billAmount)/100;
            float receivable = Float.parseFloat(receivableAmount) / 100;
            float discount = Float.parseFloat(discountAmount) / 100;
            System.out.println("应收标准费用:"+receivable);
            System.out.println("实际应收费用:"+bill);
            System.out.println("优惠金额:"+discount);
            return bill+"|"+receivable+"|"+discount;
        }
        return null;
    }
}
